package com.itheima;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.pojo.Goods;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

/*
 * 文档操作
 * 增 删 查 改
 * */
@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo03DocOperation {

    //注入高级客户端对象，用来操作es
    @Autowired
    private RestHighLevelClient client;

    /**
     * 目标：新增文档
     * 1.创建请求对象：新增文档
     * 设置索引库name
     * 设置类型type
     * 设置主键id，不设置随机生成
     * 设置请求体
     * 2.客户端发送请求，获取响应对象
     * 3.打印响应结果
     */
    @Test
    public void createDoc() throws IOException {
        //1.创建请求对象：新增文档
        IndexRequest request = new IndexRequest();
        //设置索引库名称
        request.index("heima");
        //设置type类型
        request.type("_doc");
        //设置主键id
        request.id("1");
        //构造Goods对象
        Goods goods = new Goods(1L, "小米手机", "小米", "手机", "小米", 1000.0, "图片地址");
        //对象转json
        ObjectMapper objectMapper = new ObjectMapper();
        String goodJsonStr = objectMapper.writeValueAsString(goods);
        //设置请求体.source("json请求字符串","请求体的数据类型");
        request.source(goodJsonStr, XContentType.JSON);
        //2.客户端发送请求，获取响应对象
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        //3.打印响应结果
        System.out.println("_index：："+response.getIndex());
        System.out.println("_type：："+response.getType());
        System.out.println("_id：："+response.getId());
        System.out.println("result：："+response.getResult());
    }


    /**
     * 修改文档
     * 1.创建请求对象：修改文档
     *    设置索引库name
     *    设置类型type
     *    设置主键id，必须设置
     *    设置请求体
     * 2.客户端发送请求，获取响应对象
     * 3.打印响应结果
     */
    @Test
    public void updateDoc() throws IOException {
        //1.创建请求对象：新增文档
        IndexRequest request = new IndexRequest();
        //设置索引库名称
        request.index("heima");
        //设置type类型
        request.type("_doc");
        //设置主键id
        request.id("2");
        //构造Goods对象
        Goods goods = new Goods(1L, "小米手机", "小米", "手机", "小米", 2000.0, "图片地址2");
        //对象转json
        ObjectMapper objectMapper = new ObjectMapper();
        String goodJsonStr = objectMapper.writeValueAsString(goods);
        //设置请求体.source("json请求字符串","请求体的数据类型");
        request.source(goodJsonStr, XContentType.JSON);
        //2.客户端发送请求，获取响应对象
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        //3.打印响应结果
        System.out.println("_index：："+response.getIndex());
        System.out.println("_type：："+response.getType());
        System.out.println("_id：："+response.getId());
        System.out.println("result：："+response.getResult());
    }

    /**
     * 根据id查询文档
     * 1.创建请求对象：根据id查询文档
     *    设置索引库name
     *    设置类型type
     *    设置主键id
     * 2.客户端发送请求，获取响应对象
     * 3.打印响应结果
     */
    @Test
    public void getDoc() throws IOException {
        //1.创建请求对象：根据id查询文档
        GetRequest request = new GetRequest();
        //设置索引库name
        request.index("heima");
        //设置类型type
        request.type("_doc");
        //设置主键id
        request.id("1");
        //2.客户端发送请求，获取响应对象
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        //3.打印响应结果
        //3.打印响应结果
        System.out.println("_index：："+response.getIndex());
        System.out.println("_type：："+response.getType());
        System.out.println("_id：："+response.getId());
        System.out.println("_source：："+response.getSourceAsString());
    }

    /**
     * 删除文档
     * 1.创建请求对象：删除文档
     *    设置索引库name
     *    设置类型type
     *    设置主键id
     * 2.客户端发送请求，获取响应对象
     * 3.打印响应结果
     */
    @Test
    public void deleteDoc() throws IOException {
        //1.创建请求对象：删除文档
        DeleteRequest request = new DeleteRequest();
        request.index("heima");
        request.id("1");
        request.type("_doc");
        //2.客户端发送请求，获取响应对象
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        //3.打印响应结果
        System.out.println("_index：："+response.getIndex());
        System.out.println("_type：："+response.getType());
        System.out.println("_id：："+response.getId());
        System.out.println("_result：："+response.getResult());
    }

    /**
     * 目标：批量新增
     * 1.创建请求对象：批量操作
     * 2.批量操作中设置多个新增对象IndexRequest
     * 3.客户端发送请求，获取响应对象
     * 4.打印响应结果
     */
    @Test
    public void bulkeCreat() throws IOException {
        //1.创建请求对象：批量操作
        BulkRequest request = new BulkRequest();
        //2.批量操作中设置多个新增对象IndexRequest
        IndexRequest addRequestOne = new IndexRequest().id("1").type("_doc").index("heima").source(XContentType.JSON,"id",1L,"title","大米手机","category","手机","brand","小米","price","2699.00","images","http://baidu.com");
        request.add(addRequestOne);
        IndexRequest addRequestTwo = new IndexRequest().id("2").type("_doc").index("heima").source(XContentType.JSON,"id",2L,"title","大米手机","category","手机","brand","小米","price","2699.00","images","http://baidu.com");
        request.add(addRequestTwo);
        IndexRequest addRequestThree = new IndexRequest().id("3").type("_doc").index("heima").source(XContentType.JSON,"id",3L,"title","苹果手机","category","手机","brand","苹果","price","5422.00","images","http://baidu.com");
        request.add(addRequestThree);
        IndexRequest addRequestFour = new IndexRequest().id("4").type("_doc").index("heima").source(XContentType.JSON,"id",3L,"title","华为手机","category","手机","brand","华为","price","7634.00","images","http://baidu.com");
        request.add(addRequestFour);
        //3.客户端发送请求，获取响应对象
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        //4.打印响应结果
        System.out.println("took::"+response.getTook());
        System.out.println("Items::"+response.getItems());
    }

    /**
     * 目标：批量删除
     * 1.创建请求对象：批量删除
     * 2.批量操作中设置多个删除对象DeleteRequest
     * 3.客户端发送请求，获取响应对象
     * 4.打印响应结果
     */
    @Test
    public void bulkDelete() throws IOException {
        //1.创建请求对象：批量删除
        BulkRequest request = new BulkRequest();
        //2.批量操作中设置多个删除对象DeleteRequest
        request.add(new DeleteRequest().id("1").type("_doc").index("heima"));
        request.add(new DeleteRequest().id("2").type("_doc").index("heima"));
        request.add(new DeleteRequest().id("3").type("_doc").index("heima"));
        request.add(new DeleteRequest().id("4").type("_doc").index("heima"));
        //3.客户端发送请求，获取响应对象
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        //4.打印响应结果
        System.out.println("took::"+response.getTook());
        System.out.println("Items::"+response.getItems());
    }

    /**
     * 初始化查询数据
     */
    @Test
    public void initData() throws IOException {
        //批量新增操作
        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest().type("_doc").index("heima").source(XContentType.JSON,"title","大米手机","images","http://image.leyou.com/12479122.jpg","price",3288,"category","手机","brand","小米"));
        request.add(new IndexRequest().type("_doc").index("heima").source(XContentType.JSON,"title","小米手机","images","http://image.leyou.com/12479122.jpg","price",2699,"category","手机","brand","小米"));
        request.add(new IndexRequest().type("_doc").index("heima").source(XContentType.JSON,"title","小米电视4A","images","http://image.leyou.com/12479122.jpg","price",4288,"category","手机","brand","小米"));
        request.add(new IndexRequest().type("_doc").index("heima").source(XContentType.JSON,"title","华为手机","images", "http://image.leyou.com/12479122.jpg","price", 5288,"subtitle", "小米","category","手机","brand","小米"));
        request.add(new IndexRequest().type("_doc").index("heima").source(XContentType.JSON,"title","apple手机","images","http://image.leyou.com/12479122.jpg","price",5899.00,"category","手机","brand","小米"));
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println("took::"+response.getTook());
        System.out.println("Items::"+response.getItems());
    }
}












